﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;

namespace BackupMirthToTFS
{
    public partial class NewMirthBackup : System.Web.UI.Page
    {
        public string BackupName
        {
            get { return (Page.RouteData.Values["BackupName"] != null) ? Page.RouteData.Values["BackupName"].ToString() : ""; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadMirthBackupToPage();
            }
        }

        private void LoadMirthBackupToPage()
        {
            if (BackupName.HasValue())
            {
                using (var db = new MirthBackupDataContext(Config.BackupMirthToTFSConnString))
                {
                    var backup = db.MirthBackups.FirstOrDefault(x => x.Name == BackupName);

                    if (backup != null)
                    {
                        appNameHeader.InnerText = backup.Name;
                        MirthBackupID.Value = backup.ID.ToString();
                        txtHost.Text = backup.MirthHost;
                        txtName.Text = backup.Name;
                        txtPwd.Text = backup.MirthUserPwd;
                        chkIsActive.Checked = backup.IsActive;
                        txtTfsFile.Text = backup.TfsFile;
                        txtUserName.Text = backup.MirthUserName;
                        lblLastRun.Text = backup.LastCompletedOn.Value.PrettyFormatWithTime();
                    }
                }
            }
        }

        protected void UpdateMirthBackup(object sender, EventArgs e)
        {
            var id = short.Parse(MirthBackupID.Value);
            MirthBackup backup;

            using (var db = new MirthBackupDataContext(Config.BackupMirthToTFSConnString))
            {
                backup = (id == 0) ?
                    new MirthBackup() :
                    db.MirthBackups.SingleOrDefault(x => x.ID == id);

                BuildBackupFromForm(backup);

                if (id == 0)
                {
                    backup.CreatedBy = HttpContext.Current.User.Identity.Name.ToString();
                    backup.CreatedOn = DateTime.Now;
                    db.MirthBackups.InsertOnSubmit(backup);
                }
                db.SubmitChanges();
            }

            Response.RedirectToRoute("Dashboard");
        }

        private void ShowUserCreatedDetails(MirthBackup backup)
        { 
             lblCreated.Text = "Created: {0} ({1})".FormatWith( backup.CreatedBy , backup.CreatedOn.PrettyFormatWithTime());
        }

        private void BuildBackupFromForm(MirthBackup backup)
        {
            backup.Name = txtName.Text.Trim();
            backup.TfsFile = txtTfsFile.Text.Trim();
            backup.MirthUserPwd = txtPwd.Text.Trim();
            backup.MirthUserName = txtUserName.Text.Trim();
            backup.MirthHost = txtHost.Text.Trim();
            backup.IsActive = chkIsActive.Checked;
        }
    }
}
